package com.atguigu.rabbitmq.seven;


import com.atguigu.rabbitmq.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * 声明主题--交换机
 */
public class ReceiveLogsTopic02 {

    public static final String EXCHANGE_NAME = "topic_logs";


    /**
     * 接受消息
     * @param args
     */
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();

        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        // 声明队列
        String queueName = "Q2";
        // 声明队列
        channel.queueDeclare(queueName,   false, false,false,null);
        channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");
        System.out.println("等待接收消息");

        // 接受消息消息回调
        DeliverCallback deliverCallback = ( consumerTag,message) -> {
            System.out.println(new String(message.getBody()));
            System.out.println("接受队列：" + queueName + " 绑定键：" + message.getEnvelope().getRoutingKey());
        };
        // 消费者取消接受消息时的回调


        // 接受消息
        channel.basicConsume(queueName, true,deliverCallback ,(consumerTag, message) -> {

        } );


    }
}
